package bravo.xc.com.xc_bravo_android.operation.withdraw.withdrawmain;

import android.util.Log;

import bravo.xc.com.xc_bravo_android.beans.CommonBeans;
import bravo.xc.com.xc_bravo_android.beans.ForgetInitResultBeans;
import bravo.xc.com.xc_bravo_android.beans.GetBankResultBeans;
import bravo.xc.com.xc_bravo_android.beans.GetNumResultBeans;
import bravo.xc.com.xc_bravo_android.beans.SendCodeBeans;
import bravo.xc.com.xc_bravo_android.beans.SendMoneyCodeBeans;
import bravo.xc.com.xc_bravo_android.beans.SendMoneyCodeResultBeans;
import bravo.xc.com.xc_bravo_android.beans.WithDrawBeans;
import bravo.xc.com.xc_bravo_android.beans.WithDrawResultBeans;
import bravo.xc.com.xc_bravo_android.http.HttpSingleton;
import bravo.xc.com.xc_bravo_android.http.UserOperationService;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CREATED BY YANK
 * DATE : 2017/11/7
 * MAIL : YANK.TENYOND@GMAIL.COM
 * FUNCTION :
 */

public class WithdrawPresenter extends WithdrawContract.WithdrawPresenter {

    private UserOperationService userOperationService = HttpSingleton.getInstance().service;


    @Override
    public void doWithDraw( WithDrawBeans withDrawBeans) {
        compositeSubscription.add(userOperationService.withDraw(withDrawBeans)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<WithDrawResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@onError2",String.valueOf(e));
                        view.showToast("网络错误，请重试");

                    }

                    @Override
                    public void onNext(WithDrawResultBeans withDrawResultBeans) {
                        view.showToast(withDrawResultBeans.getMsg());
                    }
                }));

    }

    @Override
    public void doGetInit(CommonBeans commonBeans) {

        compositeSubscription.add(userOperationService.getInit(commonBeans)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<WithDrawResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@onError4",String.valueOf(e));
                        view.showToast("网络错误，请重试");
                    }

                    @Override
                    public void onNext(WithDrawResultBeans withDrawResultBeans) {
                        if(withDrawResultBeans.getCode() == 1){
                            view.setNum(withDrawResultBeans.getData().getShopTotal());
                            view.setPhone(withDrawResultBeans.getData().getPhoneNum());
                            view.setAdapter(withDrawResultBeans);
                        }
                    }
                })
        );

    }

    @Override
    public void getCodes( SendMoneyCodeBeans sendMoneyCodeBeans) {
        compositeSubscription.add(userOperationService.getCode(sendMoneyCodeBeans)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SendMoneyCodeResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@onError4",String.valueOf(e));
                        view.showToast("网络错误，请重试");

                    }

                    @Override
                    public void onNext(SendMoneyCodeResultBeans sendMoneyCodeResultBeans) {
                        view.showToast(sendMoneyCodeResultBeans.getMsg());
                    }
                }));
    }


}
